﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ManageBanking.Models;

namespace ManageBanking.Controllers
{
    public class HistoryUserController : Controller
    {
        //
        // GET: /HistoryUser/
        private HistoryUserModels hum = new HistoryUserModels();


        public ActionResult ListHistory(string idaccount, string month, string year)
        {
            if (Session["Login"] != null )
            {
                Session["idaccount"] = idaccount;
                ViewData["inforacc"] = hum.InforAcc(idaccount);
                Username us1 = (Username)Session["Login"];
                if (us1.IdTypeUser == 2)
                {
                    ViewData["menuleft_top"] = "DetailAccount";
                    Username us = (Username)Session["Login"];
                    int totalTransferSent;
                    int totalTransferReceive;
                    int totalCheque;
                    int totalDepositWithdraw;
                    ViewData["idaccount"] = idaccount;
                    ViewData["saveselectmonth"] = month;
                    ViewData["saveselectyear"] = year;

                    /*------------------TransferSent-------------------------*/
                    if (month.ToLower().Equals("all"))
                    {
                        ViewData["TransferSent"] = hum.ListTransferSent(idaccount, out totalTransferSent);
                        ViewData["totalTransferSent"] = totalTransferSent == 0 ? 1 : totalTransferSent;
                    }
                    else
                    {
                        List<InforTransferSent> listsent = new List<InforTransferSent>();
                        List<InforTransferSent> listall = hum.ListTransferSent(idaccount, out totalTransferSent);
                        
                        foreach (InforTransferSent s in listall)
                        {
                            var datem = Convert.ToDateTime(s.DateCreation).ToString("yyyy-MM").Split('-');
                            if (datem[1].Equals(month) && datem[0].Equals(year))
                                    listsent.Add(s);
                        }
                        ViewData["TransferSent"] = listsent;
                        ViewData["totalTransferSent"] = listsent.Count() == 0 ? 1 : listsent.Count();
                    }


                    /*------------------TransferReceive-------------------------*/
                    if (month == null || month.ToLower().Equals("all"))
                    {
                        ViewData["TransferReceive"] = hum.ListTransferReceive(idaccount, out totalTransferReceive);
                        ViewData["totalTransferReceive"] = totalTransferReceive == 0 ? 1 : totalTransferReceive;
                    }
                    else
                    {
                        List<InforTransferReceive> listReceive = new List<InforTransferReceive>();
                        List<InforTransferReceive> listall = hum.ListTransferReceive(idaccount, out totalTransferReceive);
                        foreach (InforTransferReceive s in listall)
                        {
                            var datem = Convert.ToDateTime(s.DateCreation).ToString("yyyy-MM").Split('-');
                            if (datem[1].Equals(month) && datem[0].Equals(year))
                                    listReceive.Add(s);
                        }
                        ViewData["TransferReceive"] = listReceive;
                        ViewData["totalTransferReceive"] = listReceive.Count() == 0 ? 1 : listReceive.Count();
                    }



                    /*------------------Cheque-------------------------*/
                    if (month == null || month.ToLower().Equals("all"))
                    {
                        ViewData["Cheque"] = hum.ListCheque(idaccount, out totalCheque);
                        ViewData["totalCheque"] = totalCheque == 0 ? 1 : totalCheque;

                    }
                    else
                    {
                        List<Cheque> listCheque = new List<Cheque>();
                        List<Cheque> listall = hum.ListCheque(idaccount, out totalTransferReceive);
                        foreach (Cheque s in listall)
                        {
                            var datem = Convert.ToDateTime(s.DateCreate).ToString("yyyy-MM").Split('-');
                            if (datem[1].Equals(month) && datem[0].Equals(year))
                                listCheque.Add(s);
                        }
                        ViewData["Cheque"] = listCheque;
                        ViewData["totalCheque"] = listCheque.Count() == 0 ? 1 : listCheque.Count(); 

                    }



                    /*------------------DepositWithdraw-------------------------*/
                    if (month == null || month.ToLower().Equals("all"))
                    {
                        ViewData["DepositWithdraw"] = hum.ListDeposit_Withdraw(idaccount, out totalDepositWithdraw);
                        ViewData["totalDepositWithdraw"] = totalDepositWithdraw == 0 ? 1 : totalDepositWithdraw;
                    }
                    else
                    {
                        List<Deposit_With> listDeposit_With = new List<Deposit_With>();
                        List<Deposit_With> listall = hum.ListDeposit_Withdraw(idaccount, out totalTransferReceive);
                        foreach (Deposit_With s in listall)
                        {
                            var datem = Convert.ToDateTime(s.DateCreation).ToString("yyyy-MM").Split('-');
                            if (datem[1].Equals(month) && datem[0].Equals(year))
                                    listDeposit_With.Add(s);
                        }
                        ViewData["DepositWithdraw"] = listDeposit_With;
                        ViewData["totalDepositWithdraw"] = listDeposit_With.Count() == 0 ? 1 : listDeposit_With.Count();
                    }



                    return View();
                }
                else
                    return RedirectToAction("Home", "Login");
            }
            else
                return RedirectToAction("Home", "Login");
        }

    }
}
